﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace Snowing.Common
{
    public class DescriptionHelper
    {
        public static string GetDescriptionByValue<T>(Object value)
        {
            string result = string.Empty;
            Type enumType = typeof(T);
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo fi in fields)
            {
                if (object.Equals(fi.GetValue(value), value))
                {
                    object[] eds = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (eds.Length == 1)
                    {
                        DescriptionAttribute enumDescription = (DescriptionAttribute)eds[0];
                        result = enumDescription.Description;
                    }
                    break;
                }
            }
            return result;
        }
    }
}
